package com.liyu.config;

import com.rabbitmq.client.BuiltinExchangeType;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author 李誉
 * @date 2022-07-22 22:10
 * @description 替代（备份）交换机
 */
@Component
public class AlternateExchangeCommon {

    public static final String NORMAL_EXCHANGE = "normal.ex.hello";

    public static final String HELLO_RK = "rk.hello";

    public static final String ALTERNATE_EXCHANGE = "alternate.exhange.hello";

    public static final String ALTERNATE_QUEUE = "alternate.queue.hello";

    public static final String WARNING_RK = "#";

    public static final String WARNING_QUEUE = "warning.queue.hello";


    @Bean("normalExchange")
    public Exchange normalExchange() {
        return new ExchangeBuilder(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT.getType()).alternate(ALTERNATE_EXCHANGE).build();
    }

    @Bean("alternateExchange")
    public Exchange alternateExchange() {
        return new ExchangeBuilder(ALTERNATE_EXCHANGE, BuiltinExchangeType.TOPIC.getType()).build();
    }

    @Bean("alternateQueue")
    public Queue alternateQueue(){
        return QueueBuilder.durable(ALTERNATE_QUEUE).build();
    }

    @Bean("warningQueue")
    public Queue warningQueue() {
        return QueueBuilder.durable(WARNING_QUEUE).build();
    }

    @Bean("alternateBinding")
    public Binding alternateBinding(Exchange alternateExchange, Queue alternateQueue) {
        return BindingBuilder.bind(alternateQueue).to(alternateExchange).with(HELLO_RK).noargs();
    }

    @Bean("warnBinding")
    public Binding warnBinding(Exchange alternateExchange, Queue warningQueue) {
        return BindingBuilder.bind(warningQueue).to(alternateExchange).with(WARNING_RK).noargs();
    }

}
